/**
 * 启动脚本 - 带环境校验
 * 确保构建环境与运行环境一致
 */
'use strict';

const chalk = require('chalk');
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

// 获取环境变量
const env = process.env.REACT_APP_ENV || 'development';
const isProduction = env === 'production';

// 设置前端URL
const frontendUrl = isProduction 
  ? 'http://106.54.165.214'
  : 'http://0.0.0.0';

// 设置后端URL
const backendUrl = isProduction
  ? 'http://106.54.165.214:8000'
  : 'http://localhost:8000';

// 打印信息
console.log(chalk.green('\n=== 环境信息 ==='));
console.log(chalk.blue(`当前环境: ${env}`));
console.log(chalk.blue(`前端URL: ${frontendUrl}`));
console.log(chalk.blue(`后端URL: ${backendUrl}`));

// 测试后端连接
console.log(chalk.green('\n=== 测试后端连接 ==='));
try {
  // Windows系统使用不同的curl命令
  const testCommand = process.platform === 'win32'
    ? `curl -s -o nul -w "%{http_code}" ${backendUrl}/api/v1/test/ping`
    : `curl -s -o /dev/null -w "%{http_code}" ${backendUrl}/api/v1/test/ping`;
  
  const statusCode = execSync(testCommand).toString().trim();
  
  if (statusCode === '200') {
    console.log(chalk.green('✓ 后端连接正常'));
  } else {
    console.log(chalk.yellow(`! 后端返回状态码: ${statusCode}`));
  }
} catch (error) {
  console.log(chalk.red('✗ 后端连接失败'));
  console.log(chalk.yellow('请确保后端服务已启动'));
}

console.log(chalk.green('\n=== 启动前端服务 ===\n'));

// 启动React开发服务器
require('react-scripts/scripts/start'); 